package com.alks.function.service.impl.procurement;


import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.PcPackMtlTypeSkc;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.procurement.PcPackMtlTypeSkcDto;
import com.alks.function.data.request.procurement.PackagMaterialRequest;
import com.alks.function.data.request.procurement.PackagMaterialRequest2;
import com.alks.function.mapper.procurement.PackagMaterialDefinitionMapper;
import com.alks.function.service.procurement.PackagMaterialDefinitionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PackagMaterialDefinitionServiceImpl implements PackagMaterialDefinitionService {

    @Autowired
    private PackagMaterialDefinitionMapper packagMaterialDefinitionMapper;

    /**
     * 包材类别查询
     * @param
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getPackagMaterialList() {
        //获取公司
        SysUserTokenDTO sysUserTokenDTO = UserIdThread.get();
        String companyId = sysUserTokenDTO.getCompanyId();

        //根据公司id进行查询
        List<PcPackMtlTypeSkcDto> packagMaterialList = packagMaterialDefinitionMapper.getPackagMaterialList(companyId);

        PageInfo list = new PageInfo(packagMaterialList);
        HashMap map = new HashMap<>();
        map.put("lotNoList", packagMaterialList);
        map.put("total", list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 包材类别删除
     * @param seqNo
     * @return
     */
    @Override
    public ResponseInfo deletePackagMaterial(Integer seqNo) {
        //获取公司
        SysUserTokenDTO sysUserTokenDTO = UserIdThread.get();
        String companyId = sysUserTokenDTO.getCompanyId();

        //健壮性判断
        if (seqNo==null || companyId==null) {
            throw new ServiceErrorException("参数异常！");
        }

        Integer sn = packagMaterialDefinitionMapper.getSeqNo(seqNo,companyId);
        if(ObjectUtils.isNull(sn)){
            throw new ServiceErrorException("该包材类别不存在！");
        }else{
            //根据公司id去删除
            packagMaterialDefinitionMapper.deletePackagMaterial(seqNo,companyId);
            return ResponseInfo.ok("删除成功");
        }
    }

    /**
     * 包材类别增加
     * @param request
     * @return
     */
    @Override
    public ResponseInfo addPackagMaterial(PackagMaterialRequest request) {
        //获取公司
        String companyId = UserIdThread.get().getCompanyId();

        //健壮性判断
        if (request.getSeqNo()==null || companyId==null) {
            throw new ServiceErrorException("参数异常！");
        }
        Integer seqNo = packagMaterialDefinitionMapper.getSeqNo(request.getSeqNo(),companyId);
        if(ObjectUtils.isNotNull(seqNo)){
            throw new ServiceErrorException("该包材类别已存在！");
        }{
            PcPackMtlTypeSkc pcPackMtlTypeSkc = new PcPackMtlTypeSkc();
            BeanUtil.copyProperties(request,pcPackMtlTypeSkc);
            pcPackMtlTypeSkc.setCompanyId(companyId);
            pcPackMtlTypeSkc.setMatchFlag(null);

            packagMaterialDefinitionMapper.addPackagMaterial(pcPackMtlTypeSkc);
            return ResponseInfo.ok("新增成功");
        }
    }

    /**
     * 包材类别更新
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo updatePackagMaterial(List<PackagMaterialRequest2> request) {
        //史倩+加了自增主键id:PC_PACK_MTL_TYPE_SKC

        //获取公司
        String companyId = UserIdThread.get().getCompanyId();
        //健壮性判断
        if (companyId==null || ObjectUtils.isEmpty(request)) {
            throw new ServiceErrorException("参数异常！");
        }

        ArrayList<PcPackMtlTypeSkc> pcPackMtlTypeSkcList = new ArrayList<>();
        List<Integer> list = request.stream().map(PackagMaterialRequest2::getId).collect(Collectors.toList());
        //同一家公司的所有seqNo
        List<Integer> seqNoList = packagMaterialDefinitionMapper.getSNList(companyId);
        for (PackagMaterialRequest2 materialRequest : request) {
            //修改序号的特殊情况
            Integer seqNoBefore = packagMaterialDefinitionMapper.getSeqNoById(materialRequest.getId());
            //判断如果修改的序号和之前的一样，或者该序号不存在于该公司原有序号中，那么就正常更新
            if (seqNoBefore.equals(materialRequest.getSeqNo()) || !seqNoList.contains(materialRequest.getSeqNo())) {
                PcPackMtlTypeSkc pcPackMtlTypeSkc = new PcPackMtlTypeSkc();
                BeanUtil.copyProperties(materialRequest, pcPackMtlTypeSkc);
                pcPackMtlTypeSkc.setCompanyId(companyId);
                pcPackMtlTypeSkc.setMatchFlag(null);
                pcPackMtlTypeSkcList.add(pcPackMtlTypeSkc);
            } else {
                throw new ServiceErrorException("该包材序号已存在，请重新输入！");
            }
        }
        packagMaterialDefinitionMapper.updatePackagMaterial(pcPackMtlTypeSkcList);
        return ResponseInfo.ok("更新成功");
    }





}

